package cn.wolfcode.handle;

import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.redis.SeckillRedisKey;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

@Slf4j
@Component
@CanalTable(value = "t_order_info")
public class OrderaInfoHandler implements EntryHandler<OrderInfo> {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void insert(OrderInfo orderInfo) {
        // 将新增的order存入redis
        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_ORDER_HASH.getRealKey(""), orderInfo.getOrderNo() + "", JSON.toJSONString(orderInfo));
    }

    @Override
    public void update(OrderInfo before, OrderInfo after) {
        // 将修改后的order存入redis
        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_ORDER_HASH.getRealKey(""), after.getOrderNo() + "", JSON.toJSONString(after));
    }

    @Override
    public void delete(OrderInfo orderInfo) {
        // 在redis中删除已删除的订单信息
        redisTemplate.opsForHash().delete(SeckillRedisKey.SECKILL_ORDER_HASH.getRealKey(""), orderInfo.getOrderNo() + "");
        // 在redis中删除用户已买的商品信息
        redisTemplate.opsForSet().remove(SeckillRedisKey.SECKILL_ORDER_SET.getRealKey(orderInfo.getUserId() + ""),orderInfo.getSeckillId() + "");
    }
}